package com.asiainfo.bms.service.impl;

import com.asiainfo.bms.mapper.BookMapper;
import com.asiainfo.bms.mapper.BorrowMapper;
import com.asiainfo.bms.mapper.EmployeeMapper;
import com.asiainfo.bms.model.Book;
import com.asiainfo.bms.model.Borrow;
import com.asiainfo.bms.model.Employee;
import com.asiainfo.bms.service.BorrowService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 借阅表 服务实现类
 * </p>
 *
 * @author liu.hx
 * @since 2021-07-13
 */
@Service
public class BorrowServiceImpl extends ServiceImpl<BorrowMapper, Borrow> implements BorrowService {
    @Autowired
    private BookMapper bookMapper;
    @Autowired
    private EmployeeMapper employeeMapper;

    /**
     * 分页查询
     *
     * @param page   当前页码
     * @param limit  每页记录数
     * @param borrow 借阅
     * @return IPage类实例
     */
    @Override
    public IPage<Borrow> pageSelect(Long page, Long limit, Borrow borrow) {
        // 创建条件构造器
        LambdaQueryWrapper<Borrow> queryWrapper = Wrappers.lambdaQuery();
        if (borrow != null) {
            String isbn = borrow.getIsbn();
            String bookName = borrow.getBookName();
            String no = borrow.getNo();
            String employeeName = borrow.getEmployeeName();
            Integer status = borrow.getStatus();
            // 设置查询条件
            if (!StringUtils.isEmpty(isbn)) {
                queryWrapper.eq(Borrow::getIsbn, isbn);
            }
            if (!StringUtils.isEmpty(bookName)) {
                queryWrapper.like(Borrow::getBookName, bookName);
            }
            if (!StringUtils.isEmpty(no)) {
                queryWrapper.eq(Borrow::getNo, no);
            }
            if (!StringUtils.isEmpty(employeeName)) {
                queryWrapper.like(Borrow::getEmployeeName, employeeName);
            }
        }
        return baseMapper.selectPage(new Page<>(page, limit), queryWrapper);
    }

    /**
     * 分页查询未还图书
     *
     * @param page   当前页码
     * @param limit  每页记录数
     * @param borrow 借阅
     * @return IPage类实例
     */
    @Override
    public IPage<Borrow> pageSelectReturn(Long page, Long limit, Borrow borrow) {
        // 创建条件构造器
        LambdaQueryWrapper<Borrow> queryWrapper = Wrappers.lambdaQuery();
        if (borrow != null) {
            String isbn = borrow.getIsbn();
            String bookName = borrow.getBookName();
            String no = borrow.getNo();
            String employeeName = borrow.getEmployeeName();
            Integer status = borrow.getStatus();
            // 设置查询条件
            if (!StringUtils.isEmpty(isbn)) {
                queryWrapper.eq(Borrow::getIsbn, isbn);
            }
            if (!StringUtils.isEmpty(bookName)) {
                queryWrapper.like(Borrow::getBookName, bookName);
            }
            if (!StringUtils.isEmpty(no)) {
                queryWrapper.eq(Borrow::getNo, no);
            }
            if (!StringUtils.isEmpty(employeeName)) {
                queryWrapper.like(Borrow::getEmployeeName, employeeName);
            }
            queryWrapper.eq(Borrow::getStatus, 2);
        }
        return baseMapper.selectPage(new Page<>(page, limit), queryWrapper);
    }

    /**
     * 新增一个借阅(借书)
     *
     * @param borrow 借阅
     * @return 员工可借数量
     */
    @Override
    public Integer borrowBook(Borrow borrow) {
        //　插入一个新的借阅信息
        baseMapper.insert(borrow);
        // 通过ID获得图书和员工信息
        Book book = bookMapper.selectById(borrow.getBookId());
        Employee employee = employeeMapper.selectById(borrow.getEmployeeId());
        // 判断员工是否还可以借书
        if (employee.getBorrowedNum().equals(CAN_BORROW_NUM)) {
            // 如果不可以 返回0
            return 0;
        }
        // 判断该书是否还可借
        if (book.getLeftNum().equals(0)) {
            return -1;
        }
        // 如果可以 图书可借数量-1
        book.setLeftNum(book.getLeftNum() - 1);
        bookMapper.updateById(book);
        // 员工已借数量+1
        employee.setBorrowedNum(employee.getBorrowedNum() + 1);
        employeeMapper.updateById(employee);
        // 返回员工已借数量
        return CAN_BORROW_NUM - employee.getBorrowedNum();
    }

    /**
     * 修改一个借阅(还书)
     *
     * @param borrow 借阅
     * @return 员工可借数量
     */
    @Override
    public Integer returnBook(Borrow borrow) {
        // 判断该条借阅信息是否已经还书
        if (borrow.getStatus().equals(1)) {
            return -1;
        }
        // 通过ID获得图书和员工信息
        Book book = bookMapper.selectById(borrow.getBookId());
        Employee employee = employeeMapper.selectById(borrow.getEmployeeId());
        // 图书可借数量+1
        book.setLeftNum(book.getLeftNum() + 1);
        bookMapper.updateById(book);
        // 员工已借数量-1
        employee.setBorrowedNum(employee.getBorrowedNum() - 1);
        employeeMapper.updateById(employee);
        // 修改借阅状态
        borrow.setStatus(1);
        baseMapper.updateById(borrow);
        // 返回员工可借数量
        return CAN_BORROW_NUM - employee.getBorrowedNum();
    }
}
